package com.zhongshengnetwork.rightbe.dbservice;

import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.dbmodel.LangPublishdbModel;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

public class LangPublishService {

    //插入或者更新数据库
    public static boolean saveOrUpdate(LangPublishdbModel model){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.saveOrUpdate(model);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取总的未发布数量
    public static long getAllPublishCount(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(LangPublishdbModel.class).where("userid","==",userid) != null){
                return db.selector(LangPublishdbModel.class).where("userid","==",userid).count();
            }
        } catch (DbException e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }

    //获取微句待发布数量
    public static long getWJPublishCount(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(LangPublishdbModel.class).where("userid","==",userid) != null){
                return db.selector(LangPublishdbModel.class).where("userid","==",userid).and("datatype","==",1).count();
            }
        } catch (DbException e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }

    //获取微纸条待发布数量
    public static long getWZTPublishCount(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(LangPublishdbModel.class).where("userid","==",userid) != null){
                return db.selector(LangPublishdbModel.class).where("userid","==",userid).and("datatype","==",0).count();
            }
        } catch (DbException e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }

    //分页获取数据
    public static List<LangPublishdbModel> getData(String userid, int pageIndex, int pageRecord){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            return db.selector(LangPublishdbModel.class).where("userid","==",userid).orderBy("id", false).offset(pageIndex).limit(pageRecord).findAll();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }

    //获取单条数据
    public static LangPublishdbModel getOneData(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            return db.selector(LangPublishdbModel.class).where("userid","==",userid).orderBy("id", false).findFirst();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }

    //删除全部数据
    public static boolean deleteAllData(){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.delete(LangPublishdbModel.class);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //删除单条数据
    public static boolean deleteById(int id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.deleteById(LangPublishdbModel.class, id);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

}
